unit DiagramLabel;

interface
uses Classes, Graphics;

type
  THorzAlign = (haCenter, haLeft, haRight);
  TVertAlign = (vaCenter, vaTop, vaBottom);

  TDiagramLabel = class(TPersistent)
  private
    FOX, FOY, FDX, FDY: Double;
    FCaption: string;
    FHorzAlign: THorzAlign;
    FVertAlign: TVertAlign;
    FRotated: Boolean;
    FTransparentBkground: Boolean;
    FOnUpdate: TNotifyEvent;
    FFont: TFont;
    FCustomFont: Boolean;
    function GetLabelX: Double;
    procedure SetLabelX(AX: Double);
    function GetLabelY: Double;
    procedure SetLabelY(AY: Double);
  public
    procedure AssignDefaultFont(AFont: TFont);
    constructor Create(AOnUpdate: TNotifyEvent);
    destructor Destroy; override;
    procedure SetOXY(OX, OY: Double);
    procedure Update;
    property X: Double read GetLabelX write SetLabelX;
    property Y: Double read GetLabelY write SetLabelY;
  published
    property Font: TFont read FFont write FFont
      stored FCustomFont;
    property CustomFont: Boolean read FCustomFont write FCustomFont
      default False;
    property DX: Double read FDX write FDX;
    property DY: Double read FDY write FDY;
    property Caption: string read FCaption write FCaption;
    property HorzAlign: THorzAlign read FHorzAlign
      write FHorzAlign default haCenter;
    property VertAlign: TVertAlign read FVertAlign
      write FVertAlign default vaCenter;
    property Rotated: Boolean read FRotated write FRotated default False;
    property TransparentBkground: Boolean
      read FTransparentBkground write FTransparentBkground default False;
  end;

implementation

{ TDiagramLabel }
procedure TDiagramLabel.AssignDefaultFont(AFont: TFont);
begin
  Assert(Assigned(AFont));
  if not CustomFont then
    Font.Assign(AFont);
end;

constructor TDiagramLabel.Create(AOnUpdate: TNotifyEvent);
begin
  FOnUpdate := AOnUpdate;
  FFont := TFont.Create;
end;

destructor TDiagramLabel.Destroy;
begin
  FFont.Free;
end;

function TDiagramLabel.GetLabelX: Double;
begin
  Result := FOX + FDX;
end;

function TDiagramLabel.GetLabelY: Double;
begin
  Result := FOY + FDY;
end;

procedure TDiagramLabel.SetLabelX(AX: Double);
begin
  FDX := AX - FOX;
end;

procedure TDiagramLabel.SetLabelY(AY: Double);
begin
  FDY := AY - FOY;
end;

procedure TDiagramLabel.SetOXY(OX, OY: Double);
begin
  FOX := OX;
  FOY := OY;
end;

procedure TDiagramLabel.Update;
begin
  if Assigned(FOnUpdate) then
    FOnUpdate(Self);
end;

end.

